//
//  RestListCellFrame.m
//  iBaby365v2
//
//  Created by admin on 14/12/23.
//  Copyright (c) 2014年 suplus. All rights reserved.
//
// 昵称的字体
#define NameFont [UIFont systemFontOfSize:15]
// 正文的字体
#define TextFont [UIFont systemFontOfSize:13]
#define timeFont [UIFont systemFontOfSize:12]

#import "RestListCellFrame.h"
#import "NSDate+suplus.h"


@implementation RestListCellFrame

-(void)setReverMutableArray:(NSArray *)reverMutableArray
{
    _reverMutableArray = reverMutableArray;
    [self settingFrame];
}
- (void)setData:(NSMutableDictionary *)data
{
    _data = data;
    

    if(data[KReplyContent] != [NSNull null]){
        _reverMutableArray = data[KReplyContent];
        
    }else{
        _reverMutableArray = [NSArray array];
    }
    
    _dateId = data[kId];
    
    [self settingFrame];
 
}
/**
 *  设置frame
 */
- (void)settingFrame
{
    // 子控件之间的间距
    CGFloat padding = 5;
//    手机屏幕宽度
    CGFloat myScreenW =[UIScreen mainScreen].bounds.size.width;
    // 1.头像
    CGFloat iconX = padding;
    CGFloat iconY = padding;
    CGFloat iconW = 45;
    CGFloat iconH = 45;
    _iconF = CGRectMake(iconX, iconY, iconW, iconH);
    
//    头像右边的宽度
    CGFloat contentW =myScreenW-2*padding-iconW-padding;
    
//    self.photo.frame = _iconF;
    // 2.昵称
    // 文字的字体
//    NSLog(@"-data[kparentName]-%@",_data[kparentName]);
    CGSize nameSize = [self sizeWithText:_data[kparentName] font:NameFont maxSize:CGSizeMake(MAXFLOAT, MAXFLOAT)];
    CGFloat nameX = CGRectGetMaxX(_iconF) + padding;
    CGFloat nameY = padding;
    _nameF = CGRectMake(nameX, nameY, nameSize.width, nameSize.height);

    
    
    NSString *dateStringWithT = _data[kCreateTime];
    NSLog(@"-dateStringWithT-%@",dateStringWithT);
    NSString *dateString = [dateStringWithT stringByReplacingOccurrencesOfString:@"T" withString:@" "];
    NSLog(@"-dateString-%@",dateString);
    //    NSLog(@"-_dateTime.text-%@",_dateTime.text);
    dateString = [self dateTimeTC:dateString];
    CGSize dateTimeSize = [self sizeWithText:dateString font:timeFont maxSize:CGSizeMake(contentW-_nameF.size.width-padding, MAXFLOAT)];
    CGFloat timeX = myScreenW-padding*2-dateTimeSize.width;
    CGFloat timeY = CGRectGetMaxY(_nameF)-dateTimeSize.height;
    _dateTimeF = CGRectMake(timeX, timeY, dateTimeSize.width, dateTimeSize.height);
//    self.dateTime.frame = _dateTimeF;
    
    CGSize revrtBUttonSize = [self sizeWithText:@"回复" font:NameFont maxSize:CGSizeMake(40, MAXFLOAT)];
    CGFloat revrtBUttonX = myScreenW-padding*2-revrtBUttonSize.width;
    CGFloat revrtBUttonY = CGRectGetMaxY(_nameF)+padding;
    _revrtBUttonF = CGRectMake(revrtBUttonX, revrtBUttonY, revrtBUttonSize.width, revrtBUttonSize.height);

    //    NSLog(@"-_content.text-%@",_content.text);
//    正文的宽度
    CGFloat contentTextW= revrtBUttonX-iconW-padding*2;
    CGSize contentSize = [self sizeWithText:_data[kContent] font:TextFont maxSize:CGSizeMake(contentTextW, MAXFLOAT)];
    CGFloat contentX = nameX;
    CGFloat contentY = CGRectGetMaxY(_nameF)+padding+padding*0.5;
    _contentF = CGRectMake(contentX, contentY, contentSize.width, contentSize.height);
//    _content.frame = _contentF;
    CGFloat frameHeight =CGRectGetMaxY(_contentF);
    
    _reViewMutableArrayF = [NSMutableArray array];
    if(_reverMutableArray.count>0){
        frameHeight =frameHeight+padding*3;
        CGFloat revertViewX = nameX;
        CGFloat revertViewY = frameHeight;
        CGFloat viewStartX = 0;
        CGFloat viewStartY = 0;
//        CGFloat viewheight = 0;
        for (int i=0 ;i< _reverMutableArray.count;i++) {
            //      for (NSDictionary *dict in _reverMutableArray) {
            
            NSDictionary *dict =_reverMutableArray[i];
            NSString *replyContent = dict[@"content"];
            NSString *createTime = dict[@"createDime"];
            
            NSString *timeText = [self dateTimeTC:createTime];
            
            
            NSMutableDictionary *dictF = [NSMutableDictionary dictionary];
//            UILabel *replyContentView = [[UILabel alloc]init];
//            
//            replyContentView.font =TextFont;
//            replyContentView.text =@"回复:";
//            replyContentView.textColor = UIColorFromRGB(0x027282, 1.0);
//            [_revertView addSubview:replyContentView];
            
            CGSize replyContentSize = [self sizeWithText:@"回复:" font:TextFont maxSize:CGSizeMake(80, MAXFLOAT)];
            CGFloat replyContentX = viewStartX;
            CGFloat replyContentY = viewStartY;
            CGRect _replyContentF = CGRectMake(replyContentX, replyContentY, replyContentSize.width, replyContentSize.height);
            NSValue *replyContentFValue = [NSValue valueWithCGRect:_replyContentF];
            dictF[@"replyContentFValue"] = replyContentFValue;
//            replyContentView.frame = _replyContentF;
            
//            UILabel *timeContentView = [[UILabel alloc]init];
//            //            replyContentView.numberOfLines = 0;
//            timeContentView.font =timeFont;
//            timeContentView.text =timeText;
//            timeContentView.textColor = [UIColor grayColor];
//            [_revertView addSubview:timeContentView];
            
            CGSize timeContentSize = [self sizeWithText:timeText font:timeFont maxSize:CGSizeMake(contentW-_replyContentF.size.width-padding*2, MAXFLOAT)];
            CGFloat timeContentX = contentW-padding-timeContentSize.width;
            CGFloat timeContentY = CGRectGetMaxY(_replyContentF)-timeContentSize.height;
            CGRect _timeContentF = CGRectMake(timeContentX, timeContentY, timeContentSize.width, timeContentSize.height);
            NSValue *timeContentFValue = [NSValue valueWithCGRect:_timeContentF];
            dictF[@"timeContentFValue"] = timeContentFValue;
            
            CGSize revertContentSize = [self sizeWithText:replyContent font:TextFont maxSize:CGSizeMake(contentTextW, MAXFLOAT)];
            CGFloat revertContentX = viewStartX;
            CGFloat revertContentY = CGRectGetMaxY(_replyContentF)+padding*2;
            CGRect _revertContentF = CGRectMake(revertContentX, revertContentY, revertContentSize.width, revertContentSize.height);
            NSValue *revertContentFValue = [NSValue valueWithCGRect:_revertContentF];
            dictF[@"revertContentFValue"] = revertContentFValue;
            
            [_reViewMutableArrayF addObject:dictF];
            
//            revertContentView.frame = _revertContentF;
            NSLog(@"CGRectGetMaxY(_revertContentF)+padding--------->%f",CGRectGetMaxY(_revertContentF)+padding);
//            viewheight =CGRectGetMaxY(_revertContentF)+padding*2;
            NSLog(@"frameHeight--------->%f",frameHeight);
            viewStartY =CGRectGetMaxY(_revertContentF)+padding*2;
        }
        frameHeight =frameHeight+viewStartY-padding*2;
        _revertViewF = CGRectMake(revertViewX, revertViewY, contentW, frameHeight-revertViewY);
    }
    
    self.cellHeight = frameHeight+padding*2>CGRectGetMaxY(_iconF)+padding*2?frameHeight+padding*2:CGRectGetMaxY(_iconF)+padding*2;
}
- (NSString *)dateTimeTC:(NSString *)created_at
{
    // _created_at == Fri May 09 16:30:34 +0800 2014
    // 1.获得微博的发送时间
    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
    fmt.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    fmt.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
    NSDate *createdDate = [fmt dateFromString:created_at];
    
    // 2..判断微博发送时间 和 现在时间 的差距
    if (createdDate.isToday) { // 今天
        if (createdDate.deltaWithNow.hour >= 1) {
            return [NSString stringWithFormat:@"%d小时前", createdDate.deltaWithNow.hour];
        } else if (createdDate.deltaWithNow.minute >= 1) {
            return [NSString stringWithFormat:@"%d分钟前", createdDate.deltaWithNow.minute];
        } else {
            return @"刚刚";
        }
    } else if (createdDate.isYesterday) { // 昨天
        fmt.dateFormat = @"昨天 HH:mm";
        return [fmt stringFromDate:createdDate];
    } else if (createdDate.isThisYear) { // 今年(至少是前天)
        fmt.dateFormat = @"MM-dd HH:mm";
        return [fmt stringFromDate:createdDate];
    } else { // 非今年
        fmt.dateFormat = @"yyyy-MM-dd HH:mm";
        return [fmt stringFromDate:createdDate];
    }
}

/**
 *  计算文字尺寸
 *
 *  @param text    需要计算尺寸的文字
 *  @param font    文字的字体
 *  @param maxSize 文字的最大尺寸
 */
- (CGSize)sizeWithText:(NSString *)text font:(UIFont *)font maxSize:(CGSize)maxSize
{
    NSDictionary *attrs = @{NSFontAttributeName : font};
    return [text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size;
}

@end
